package robocup.component.tactics;

import robocup.component.actions.TurnAction;
import robocup.component.geometry.Vektor;
import robocup.component.worldobjects.Ball;

/**
 * The {@link SearchBallState} is triggered when a player lost the ball. It
 * triggeres the algorithm that searchs the ball. This state is also the default
 * state triggered when no other state is possible (which should never ever
 * happen!!). If you see this state even so the player knows where the ball is,
 * you might probably have forgotten to define a state for a given world.
 */
public class SearchBallState
    extends AbstractState {

  /**
   * SearchBallState always makes the player look towards the ball!
   * 
   * @param parent -
   *          the StateEvaluation (parent) object
   */
  public SearchBallState(
      final StateEvaluation parent) {

    super(parent);
    this.state = STATES.SEARCH_BALL;
  }

  @Override
  public boolean preCondition() {

    return !this.checkMyBall();
  }

  /**
   * Dummy....
   */
  @Override
  protected double successProbability() {

    return 1;
  }

  /**
   * Because this is a dummy state - the values for assessment are very low!
   */
  @Override
  protected double successBenefit() {

    return (!this.getWorld().getBallRef().isReliable()) ? 0.95 : 0.00001;
  }

  @Override
  public void calculateMainAction() {

    final Ball ball = this.getWorld().getBallRef();

    // if ball is reliable look to the ball
    if (ball.isReliable()) {
      final double angleToBall = Vektor.normalize(this.getWorld().getNextPosition(
          ball).sub(this.getWorld().getMyNextPosition()).getAngle() - this
          .getWorld().getBodyAngle());
      double turnAngle;
      turnAngle = this.getWorld().getSelfRef().getAngleForTurn(angleToBall);

      this.mainAction = new TurnAction(turnAngle);

    }
    else {

      // turn by 45.0 degree
      final double angleToTurn = 45 - 10.0;
      final double correctedTurnAngle = this.getWorld().getSelfRef().getAngleForTurn(
          angleToTurn);

      this.mainAction = new TurnAction(correctedTurnAngle);
    }
  }

}
